package com.bykj.taketurns.findfragment.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.findfragment.activity.SecondaryActivity;
import com.bykj.taketurns.findfragment.adapter.SecondaryVideoAdapter;
import com.bykj.taketurns.findfragment.bean.SecondaryVideoBean;
import com.bykj.taketurns.utils.Constant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/8/15.
 */

public class SecondaryVideoFragment extends Fragment implements View.OnClickListener {
    private TextView title_video_secondary;
    private String title;
    private SecondaryActivity activity;
    private String typeid;
    private RecyclerView rv_data_secondary;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find_secondary_video,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initHeader();
        initWidget();
        setWidgetState();
    }

    private void initHeader() {
        activity = (SecondaryActivity) getActivity();
    }

    private void initWidget() {

        title_video_secondary = getActivity().findViewById(R.id.title_video_secondary);
        rv_data_secondary = getActivity().findViewById(R.id.rv_data_secondary);

        getActivity().findViewById(R.id.word_video_secondary).setOnClickListener(this);
        getActivity().findViewById(R.id.iv_back_secondary_video).setOnClickListener(this);

    }

    private void setWidgetState() {
        rv_data_secondary.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_data_secondary.setHasFixedSize(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        postData();
    }

    //进行网络请求数据
    public void postData() {

        if (getArguments() != null){
            Bundle bundle = getArguments();
            title = bundle.getString("title");
            typeid = bundle.getString("typeid");
        }

        title_video_secondary.setText(title);
        activity.map.put("typeid", typeid);

        OkGo.get(Constant.JHC_SERVICE +
                Constant.ServiceConstant.FIND_SECONDARY_VIDEO + Constant.SetUrl(activity.map))     // 请求方式和请求url
                .tag(this)
                .execute(new StringCallback() {

                    private SecondaryVideoBean secondaryVideoBean;

                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        secondaryVideoBean = activity.gson.fromJson(s, SecondaryVideoBean.class);

                        rv_data_secondary.setAdapter(new SecondaryVideoAdapter(getActivity(), secondaryVideoBean.getOneList()));

                    }
                });
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.word_video_secondary:
                FragmentManager manager = getActivity().getSupportFragmentManager();
                SecondaryTextFragment secondaryTextFragment = (SecondaryTextFragment) manager.findFragmentByTag("text");

                Bundle bundle = new Bundle();
                bundle.putString("typeid", typeid);
                bundle.putString("title",title);
                secondaryTextFragment.setArguments(bundle);

                ((SecondaryActivity)getActivity()).applyRotation(true, secondaryTextFragment, 0, 90,
                        SecondaryVideoFragment.this);
                break;
            case R.id.iv_back_secondary_video:
                getActivity().finish();
                break;
        }
    }
}
